package Test.stream流测试;

import Test.Test02.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author kirito
 * @Date 2023/12/12 13:48
 * @PackageName:Test.stream流测试
 * @ClassName: test01
 * @Description:
 * @Version 1.0
 */
public class test01 {
    public static void main(String[] args) {
        Student a = new Student("a", 1);
        Student b = new Student("b", 2);
        Student c = new Student("c", 3);
        Student d = new Student("d", 4);
        Student e = new Student("e", 5);
        Student A = new Student("a", 10);
        Student A_null = new Student("a", null);
        List<Student> list1 = new ArrayList<>();
        List<Student> list2 = new ArrayList<>();
        List<String> collect = list2.stream()
                .filter(item -> item != null)
                .map(item -> item.getName())
                .collect(Collectors.toList());
        System.out.println(collect);
//        list1.add(a);
//        list1.add(b);
//        list1.add(c);
////        list2.add(a);
//        list2.add(d);
//        list2.add(e);
////        list2.add(A);
//        list2.add(A_null);
//        System.out.println(list1);
//        for (Student s1 : list1) {
//            for (Student s2 : list2) {
//                if (s1.getName().equals(s2.getName())) {
//                    s1.setAge(s2.getAge());
//                }
//            }
//        }
//        list1.forEach(item ->{
//            list2.forEach(item2 -> {
//                if (item.getName().equals(item2.getName())) {
//                    item.setAge(item2.getAge());
//                }
//            });
//        });
//        List<Student> collect = list2.stream()
//                .flatMap(item -> list1.stream()
//                        .filter(item2 -> item.getName().equals(item2.getName())
//                                && !item.getAge().equals(item2.getAge())))
//                .collect(Collectors.toList());
//        System.out.println(collect);
        list1.stream()
                    .forEach(item ->{
                        list2.stream()
                                .filter(item2 -> item2.getName().equals(item.getName()))
                                .peek(student -> {
                                    if (student.getAge() == null) {
                                        try {
                                            throw new Exception("空异常");
                                        } catch (Exception ex) {
                                            throw new RuntimeException(ex);
                                        }
                                    }
                                })
                                .findFirst()
                                .ifPresent(item2 -> item.setAge(item2.getAge()));
                    });

        System.out.println(list1);

    }
}
